package com.gxc;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Executors创建的4种线程池的使用.
 *
 * @author 宫新程
 * @since 2018/12/10 10:29 AM
 */
public class Test_Thread_2 {

  static Runnable runnable = () -> {
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName());
  };

  public static void main(String[] args) {
    ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
    ExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
    ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();

//    execute(cachedThreadPool);
 //   execute(fixedThreadPool);
//    execute(scheduledThreadPool);
    execute(singleThreadExecutor);
  }

  private static void execute(ExecutorService executorService) {
    for (int i = 0; i < 10; i++) {
      executorService.execute(runnable);
    }
  }
}
